package cn.com.shaom.learning.sb.web.data.jdbc.dao;

import cn.com.shaom.learning.sb.web.data.jdbc.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Shaom
 */
@Repository
public class UserDaoImpl implements IUserDao {

    //直接注入使用
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<User> getAll() {
        String sql = "SELECT a.* FROM learning_springBoot.t_user a";
        List<User> lists = jdbcTemplate.query(sql, (rs, index) -> {
            return getUser(rs);
        });
        return lists;
    }

    private User getUser(ResultSet rs) throws SQLException {
        User user = new User();
        user.setId(rs.getString("id"));
        user.setName(rs.getString("name"));
        user.setPwd(rs.getString("password"));
        return user;
    }

}